//非阻塞IO
#include<iostream>
#include<vector>
#include<cstring>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
using namespace std;

void SetNonBlock(int fd)
{
  int f1 = fcntl(fd,F_GETFL);//返回当前文件描述符的属性（位图）
  if(f1<0)
  {
    cout<<"fcntl fail"<<endl;
    return;
  }
  fcntl(fd, F_SETFL,f1|O_NONBLOCK);//把新的参数设置进去
}

int main()
{
  SetNonBlock(0);
  while(1)
  {
    char buffer[1024];
    ssize_t s = read(0, buffer,sizeof(buffer)-1);
    if(s>0)
    {
      buffer[s] = 0;
      write(1,buffer,strlen(buffer)); 
      cout<<"read success  "<< s<<" "<<errno <<endl;
    }
    else 
    {
      if(errno == EAGAIN||errno==EWOULDBLOCK)
      {
        cout<<"数据没准备好！！"<<endl;
        cout<<"read failed  "<<s<<" " <<errno<<endl;
        sleep(1);
        continue;
      }
    }

  }
  return 0;
}
